﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web;

namespace Shuba.Web.master
{
    public partial class holiday_add : basePage
    {
        string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtHolidayDate.ClientID;
            this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("MSTR_HOLIDAY");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("MSTR_HOLIDAY");

                //RegVal1.ValidationExpression = "^[\\d\\s]+$";//"^[0-9\\s]+$";

                if (Request.QueryString["a"] == "ed")
                {
                    BindData();
                }
                else
                {
                    txtHolidayDate.Text = ProcessingDate().ToString("dd MMMM yyyy");
                }
            }
        }

        private void BindData()
        {
            hiHolidayDate.Value = Request.QueryString["id"];

            Shuba.Business.Holiday branch = new Shuba.Business.Holiday();
            Shuba.Model.HolidayInfo bi;

            bi = branch.Get(Convert.ToDateTime(Request.QueryString["id"]));
            txtHolidayDate.Text = bi.HOLIDAY_DATE.ToString("dd MMMM yyyy");
            txtHoliday.Text = bi.HOLIDAY_NAME;

            if (bi.IS_ACTIVE == "0") btnSubmit.Text = "Activate"; else btnSubmit.Text = "Submit";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Shuba.Business.Holiday bl = new Shuba.Business.Holiday();
            Shuba.Model.HolidayInfo bi = new Shuba.Model.HolidayInfo();

            string ActType = "";
            DateTime Orig = Convert.ToDateTime(txtHolidayDate.Text);

            if (Request.QueryString["a"] != "ed")
            {
                if (bl.Get(Convert.ToDateTime(txtHolidayDate.Text)).HOLIDAY_NAME == null)
                {
                    ActType = "INSERT";
                }
                else
                {
                    msg = "Holiday Already Exist";
                    Alert(msg);
                }
            }
            else
            {
                ActType = "UPDATE";
                Orig = Convert.ToDateTime(hiHolidayDate.Value);
            }

            if (msg == "")
            {
                bi.HOLIDAY_DATE = Convert.ToDateTime(txtHolidayDate.Text);
                bi.HOLIDAY_NAME = txtHoliday.Text.Trim().Replace("'", "`").Replace("\"", "");
                bi.ENTRY_BY = HttpContext.Current.User.Identity.Name;
                bi.ENTRY_DATE = ProcessingDate();
                bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
                bi.UPDATE_DATE = ProcessingDate();
                bi.ACT_TYPE = ActType;
                bi.IS_ACTIVE = "1";
                bi.HOLIDAY_DATE_ORIG = Orig;
                try
                {
                    bl.Save(bi);

                    AddLog(ActType, ActType + " HOLIDAY");
                    msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
                    AlertAndRedirect(msg, "holiday_list.aspx");
                }
                catch (Exception ex)
                {
                    ErrBox.Visible = true;
                    ErrMsg.Text = ex.Message;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("holiday_list.aspx");
        }
    }
}
